/* 方法一 -------------------------------------------*/
// vue2响应式原理
var Book = {name:"钢铁是怎样炼成的"}

// 劫持
objProxy(Book,"name")

// 闭包封装
function objProxy(obj,key){
    var val = obj[key]
    Object.defineProperty(obj, key, {
        set: function (newVal) {
            val = newVal
            console.log('你取了一个书名叫做' + newVal)
        },
        get: function () {
            return '《' + val + '》'
        }
    })
}

// 测试
console.log(Book); // {}
console.log(Book.name); // "《钢铁是怎样炼成的》"
Book.name = "三国演义";  //你取了一个书名叫做三国演义
console.log(Book.name); // "《三国演义》"

/* 方法二 -------------------------------------------*/
// Proxy暂不支持IE
// vue3响应式原理
var Book = {name:"钢铁是怎样炼成的"}
var proxyBook = new Proxy(Book, {
	set: function (target, prop, newVal) {
	    target[prop] = newVal
	    console.log('你取了一个书名叫做' + newVal)
	},
	get: function (target, prop) {
	    return '《' + target[prop] + '》'
	}
})

//测试
console.log(proxyBook);	//Proxy {name: "钢铁是怎样炼成的"}
console.log(proxyBook.name); //"《钢铁是怎样炼成的》"
proxyBook.name = "水许传"; //你取了一个书名叫做水许传
proxyBook.price = 68;	//你取了一个书名叫做68
console.log(proxyBook);	//Proxy {name: "水浒传", price: 68}
